def is_prime(n):
    if n < 2:
        return False
    for i in range(2, int(n**0.5) + 1):
        if n % i == 0:
            return False
    return True

def find_max_prime(num):
    num_str = str(num)
    max_prime = 0
    for start in range(len(num_str)):
        for end in range(start + 1, len(num_str) + 1):
            candidate = int(num_str[start:end])
            if candidate > max_prime and is_prime(candidate):
                max_prime = candidate
    return max_prime

num = int(input("Введите четырехзначное натуральное число: "))

if num < 1000 or num > 9999:
    print(0)
else:
    result = find_max_prime(num)
    print(result if result > 0 else 0)
